package edu.hawaii.duedates.results;

import java.util.Comparator;

/**
 * Stores information about borrowed items.
 * 
 * @author Arthur Shum and Erin Kim
 */
public class BorrowedItem {

  /**
   * A Comparator object (defined by an anonymous nested class) that allows BorrowedItem objects to
   * be compared according to their due dates (yyyymmddDueDate).
   */
  public static final Comparator<BorrowedItem> dueDateComparator = new Comparator<BorrowedItem>() {
    public int compare(BorrowedItem item1, BorrowedItem item2) {
      int comp = (item1.yyyymmddDueDate).compareTo(item2.yyyymmddDueDate);
      if (comp != 0) {
        return comp;
      }
      // Compare the rest of the fields so that two BorrowedItem objects compare to zero
      // if and only if all their fields are equal.
      String item1Info = item1.libraryName.concat(item1.formattedResults);
      String item2Info = item2.libraryName.concat(item2.formattedResults);
      comp = item1Info.compareTo(item2Info);
      return comp;
    }
  };

  /**
   * A Comparator object (defined by an anonymous nested class) that allows BorrowedItem objects to
   * be compared according to the names of the libraries that they're from (libraryName).
   */
  public static final Comparator<BorrowedItem> libraryComparator = new Comparator<BorrowedItem>() {
    public int compare(BorrowedItem item1, BorrowedItem item2) {
      int comp = item1.libraryName.compareTo(item2.libraryName);
      if (comp != 0) {
        return comp;
      }
      // Compare the rest of the fields so that two BorrowedItem objects compare to zero
      // if and only if all their fields are equal.
      String item1Info = item1.yyyymmddDueDate.concat(item1.formattedResults);
      String item2Info = item2.yyyymmddDueDate.concat(item2.formattedResults);
      comp = item1Info.compareTo(item2Info);
      return comp;
    }
  };

  /** The name of the library that the borrowed item is from. */
  private final String libraryName;

  /** The item's due date in YYYYMMDD format. */
  private final String yyyymmddDueDate;

  /** A ready-to-print String containing information about the borrowed item. */
  private final String formattedResults;

  /**
   * Constructs a BorrowedItem object.
   * 
   * @param libraryName The name of the library that the item is from.
   * @param yyyymmddDueDate The item's due date in YYYYMMDD format.
   * @param formattedResults A ready-to-print String containing information about the borrowed item.
   */
  public BorrowedItem(String libraryName, String yyyymmddDueDate, String formattedResults) {
    if (libraryName == null || yyyymmddDueDate == null || formattedResults == null) {
      // Is this necessary?  I forget why I put this in here.
      throw new NullPointerException();
    }
    this.libraryName = libraryName;
    this.yyyymmddDueDate = yyyymmddDueDate;
    this.formattedResults = formattedResults;
  }

  /**
   * Gets the name of the library that the borrowed item is from.
   * 
   * @return The name of the library that the borrowed item is from..
   */
  public String getLibraryName() {
    return this.libraryName;
  }

  /**
   * Gets the item's due date in YYYYMMDD format.
   * 
   * @return The item's due date in YYYYMMDD format.
   */
  public String getYyyymmddDueDate() {
    return this.yyyymmddDueDate;
  }

  /**
   * Gets a ready-to-print String containing information about the borrowed item.
   * 
   * @return A ready-to-print String containing information about the borrowed item.
   */
  public String getFormattedResults() {
    return this.formattedResults;
  }

  /**
   * Converts a BorrowedItem object into a ready-to-print string containing information about the
   * borrowed item.
   * 
   * @return A ready-to-print string containing information about the item.
   */
  @Override
  public String toString() {
    return this.formattedResults;
  }

}
